/**
 * Created by WT-SUN on 2017/5/22.
 */
$(function () {
    var rId = $.getQueryParam('rId');
    $("#monthReportId").val(rId);
    var rectifyStateIdArr=[{id:"0",text:"整改中"},{id:"1",text:"已整改"}];
    $('#rectifyStateId').combobox(
        {
            data:rectifyStateIdArr,
            valueField:'id',
            textField:'text',
            editable: false
        }
    );
    $('#btn_save').on('click', function () {
        submitForm();
    });
    $("#rectifyDirectorId").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        }]
    });

    $('#projectId').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                var me = this;
                WindowManager.openLayer({
                    type: 2,
                    title: '选择项目',
                    area: ['800px', '500px'],
                    content: ROOT_PATH + '/threeSimultaneities/projectInfo/projectQueryList.shtml',
                    callBack:getSelectedProject
                });
            }
        }]
    });
    
    var THREEREPORTSPECIALTY=CONSTANT.DictionaryTypeDefConstants.THREEREPORTSPECIALTY;
    $('#specialtyId').combobox({
        url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code='+THREEREPORTSPECIALTY,
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text',
        onSelect: function(record){
            $('#problemTypeId').combobox({
                url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDatas.shtml?data1='+record.code,
                required:true,
                editable:false,
                valueField:'id',
                textField:'text'
            });
        }
    });
});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
var getSelectedUser = function (data) {
    if(data.length>0){
        $("#rectifyDirectorId").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
    }
};

var getSelectedProject = function (data) {
    $('#projectId').textbox('setValue', data[0].id).textbox('setText', data[0].name);//项目名称
};

/**
 * 提交表单
 */
function submitForm() {
    $('#rInfoForm').form('submit', {
        url: ROOT_PATH + '/threeSimultaneities/report/insertInfoSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}

function queryComboData(code,queryId) {
    /*var EMPTY_OPTION = {
     id: '',
     text: '请选择'
     };*/
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                /*loadFilter: function (data) {
                 data.unshift(EMPTY_OPTION);
                 return data;
                 },*/
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200'
            });
        }
    });
}